package org.mespilus.mespilusweb.ui.usermanagement.client.events;


import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;

public class UserManagementBus {
	protected static UserManagementBus instance;
	protected HandlerManager eventBus;
	
	protected UserManagementBus() {
		eventBus = new HandlerManager(this);
	}
	
	public static UserManagementBus getInstance(){
		if(instance==null) instance = new UserManagementBus();
		return instance;
	}
	
	public void registerToAddUserChange(final AddMemberHandler handler){
		eventBus.addHandler(AddMemberEvent.getTYPE(), new AddMemberHandler(){
			@Override
			public void processAddMember(AddMemberEvent event) {
				handler.processAddMember(event);
			}
		});
	}
	
	public void fireUserAddEvent(GwtEvent<AddMemberHandler> event){
		eventBus.fireEvent(event);
	}

	public void registerToUserListChange(final UserListChangedHandler handler) {
		eventBus.addHandler(UserListChangedEvent.getTYPE(), new UserListChangedHandler(){
			@Override
			public void processUserListChanged(UserListChangedEvent event) {
				handler.processUserListChanged(event);
			}
		});
	}
	
	public void fireUserListChangedEvent(GwtEvent<UserListChangedHandler> event){
		eventBus.fireEvent(event);
	}
	
	public void registerToUserListGet(final UserListGetHandler handler){
		eventBus.addHandler(UserListGetEvent.getTYPE(), new UserListGetHandler(){
			@Override
			public void processGetList(UserListGetEvent event) {
				handler.processGetList(event);
			}			
		});
	}
	
	public void fireUserListGetEvent(GwtEvent<UserListGetHandler> event){
		eventBus.fireEvent(event);
	}
	
	public void registerToUserListSearch(final MemberListSearchHandler handler){
		eventBus.addHandler(MemberListSearchEvent.getTYPE(), new MemberListSearchHandler(){
			@Override
			public void processMemberSelected(MemberListSearchEvent event) {
				handler.processMemberSelected(event);
			}		
		});
	}
	
	public void fireUserListSearchdEvent(GwtEvent<MemberListSearchHandler> event){
		eventBus.fireEvent(event);
	}
	
	public void registerToMemberFound(final MemberFoundHandler handler){
		eventBus.addHandler(MemberFoundEvent.getTYPE(), new MemberFoundHandler() {
			@Override
			public void processMemberFound(MemberFoundEvent event) {
				handler.processMemberFound(event);
			}
		});
	}
	
	public void fireMemberFoundEvent(GwtEvent<MemberFoundHandler> event){
		eventBus.fireEvent(event);
	}
}
